package com.example.springboottest.example.map;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.*;

/**
 * EnumSet与EnumMap不同，是个抽象类，不能直接实例
 */
public class EnumSetTest {
    public static void main(String[] args) {
        Worker[] workers = new Worker[]{
                new Worker("张三", EnumSet.of(
                        Day.MONDAY, Day.TUESDAY, Day.WEDNESDAY, Day.FRIDAY)),
                new Worker("李四", EnumSet.of(
                        Day.TUESDAY, Day.THURSDAY, Day.SATURDAY)),
                new Worker("王五", EnumSet.of(Day.TUESDAY, Day.THURSDAY)),
        };

        //问题1  哪些天没有一个人来？
        Set<Day> days = EnumSet.allOf(Day.class);
        for (Worker w : workers) {
            days.removeAll(w.getAvailableDays());
        }
        System.out.println("哪些天没有一个人来：" + days);

        //问题2  有哪些天至少会有一人来？
        days = EnumSet.noneOf(Day.class);
        for (Worker w : workers) {
            days.addAll(w.getAvailableDays());
        }
        System.out.println("有哪些天至少会有一人来：" + days);

        //问题3  有哪些天所有人都会来？
        days = EnumSet.allOf(Day.class);
        for (Worker w : workers) {
            days.retainAll(w.getAvailableDays());
        }
        System.out.println("有哪些天所有人都会来：" + days);

        //问题4  哪些人周一周二都会来？
        Set<Worker> availableWorkers = new HashSet<Worker>();
        for (Worker w : workers) {
            if (w.getAvailableDays().containsAll(
                    EnumSet.of(Day.MONDAY, Day.TUESDAY))) {
                availableWorkers.add(w);
            }
        }
        for (Worker w : availableWorkers) {
            System.out.println("哪些人周一周二都会来：" + w.getName());
        }

        //问题5  哪些天会来两个人？
        Map<Day, Integer> countMap = new EnumMap<>(Day.class);
        for (Worker w : workers) {
            for (Day d : w.getAvailableDays()) {
                Integer count = countMap.get(d);
                countMap.put(d, count == null ? 1 : count + 1);
            }
        }
        days = EnumSet.noneOf(Day.class);
        for (Map.Entry<Day, Integer> entry : countMap.entrySet()) {
            if (entry.getValue() >= 2) {
                days.add(entry.getKey());
            }
        }
        System.out.println("哪些天会来两个人：" + days);
    }
}

enum Day {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}

class Worker {
    //名称
    String name;

    public Set<Day> getAvailableDays() {
        return availableDays;
    }

    public void setAvailableDays(Set<Day> availableDays) {
        this.availableDays = availableDays;
    }

    //工作日
    Set<Day> availableDays;

    public Worker(String name, Set<Day> availableDays) {
        this.name = name;
        this.availableDays = availableDays;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
